package com.ha.garbage.adapter;

import android.app.Dialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.ha.garbage.R;
import com.ha.garbage.model.OrderVO;
import com.ha.garbage.model.Result;
import com.ha.garbage.service.OrderService;
import com.ha.garbage.utils.UserRetrofit;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class OrderListAdapter  extends RecyclerView.Adapter<OrderListAdapter.MyHolder>{

    private List<OrderVO> orderVOList = new ArrayList<>();

    public OrderListAdapter(List<OrderVO> orderVOList) {
        this.orderVOList = orderVOList;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate((R.layout.order_item), null);
        return new OrderListAdapter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {

        //数据绑定
        OrderVO orderVO = orderVOList.get(position);
        //设置数据
        if(orderVO.getStatus() ==1 ){
            holder.imageView.setImageResource(R.drawable.baseline_star_border_24);
            holder.tv_mine_status.setText("已提交管理员");
        }else if(orderVO.getStatus() == 2){
            holder.imageView.setImageResource(R.drawable.baseline_star_border_24_red);
            holder.tv_mine_status.setText("已发货");
        }else if(orderVO.getStatus() == 3){
            holder.imageView.setImageResource(R.drawable.baseline_check_24);
            holder.tv_mine_status.setText("已收货");
        }else {
            holder.imageView.setImageResource(R.drawable.baseline_disabled_by_default_24);
            holder.tv_mine_status.setText("已取消");
        }
        holder.tv_mine_time.setText(orderVO.getCreateTime());
        holder.tv_mine_gift.setText(orderVO.getGiftName());

        Retrofit retrofit  = UserRetrofit.retrofit();
        OrderService orderService = retrofit.create(OrderService.class);
        //确认收货
        holder.btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(orderVO.getStatus() == 4 || orderVO.getStatus() == 3){
                    return;
                }
                new AlertDialog.Builder(v.getContext())
                        .setTitle("提示")
                        .setMessage("是否确认收货")
                        .setNegativeButton("取消",null)

                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {


                                Call<Result> resultCall = orderService.setStatus(3, orderVO.getId());
                                resultCall.enqueue(new Callback<Result>() {
                                    @Override
                                    public void onResponse(Call<Result> call, Response<Result> response) {
                                        if(response.isSuccessful()){
                                            Result result = response.body();
                                            if(result.getCode() == 1){
                                                //状态修改成功
                                                orderVO.setStatus(3);
                                                notifyItemChanged(position);
                                            }else {
                                                new AlertDialog.Builder(v.getContext())
                                                        .setMessage(result.getMsg())
                                                        .setPositiveButton("知道了",null)
                                                        .show();
                                            }
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<Result> call, Throwable t) {

                                        System.out.println("访问路径为："+call.request().url().toString());
                                        System.out.println("出错原因："+t.toString());
                                    }
                                });
                            }
                        })
                        .show();

            }
        });
        //取消订单
        holder.btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(orderVO.getStatus() == 4 || orderVO.getStatus() == 3){
                    return;
                }

          new AlertDialog.Builder(v.getContext())
                  .setNegativeButton("取消",null)
                  .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {

                          Call<Result> resultCall = orderService.setStatus(4, orderVO.getId());
                          resultCall.enqueue(new Callback<Result>() {
                              @Override
                              public void onResponse(Call<Result> call, Response<Result> response) {
                                  if(response.isSuccessful()){
                                      Result result = response.body();
                                      if(result.getCode() == 1){
                                          //状态修改成功
                                          orderVO.setStatus(4);
                                          notifyItemChanged(position);
                                      }else {
                                          new AlertDialog.Builder(v.getContext())
                                                  .setMessage(result.getMsg())
                                                  .setPositiveButton("知道了",null)
                                                  .show();
                                      }
                                  }
                              }

                              @Override
                              public void onFailure(Call<Result> call, Throwable t) {

                                  System.out.println("访问路径为："+call.request().url().toString());
                                  System.out.println("出错原因："+t.toString());
                              }
                          });
                      }
                  })
                  .setTitle("提示")
                  .setMessage("是否取消该订单")
                  .show();


            }
        });



    }

    @Override
    public int getItemCount() {
        return orderVOList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder{

        TextView tv_mine_gift;
        TextView tv_mine_time;
        TextView tv_mine_status;
        Button btn_cancel;
        Button btn_confirm;
        ImageView imageView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            //初始化控件
           tv_mine_time = itemView.findViewById(R.id.tv_mime_time);
            tv_mine_status = itemView.findViewById(R.id.tv_mime_status);
            tv_mine_gift = itemView.findViewById(R.id.tv_mime_gift);
            imageView = itemView.findViewById(R.id.img_order);
            btn_cancel = itemView.findViewById(R.id.btn_cancel);
            btn_confirm = itemView.findViewById(R.id.btn_confirm);

        }
    }
}
